﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.ComponentModel.Composition;

using NuMetaheuristics.Genotypes;
using NuMetaheuristics.Operators.Vector;
using NuMetaheuristics.Operators.Value;

namespace NuMetaheuristics.Contexts.Vector.Double
{
    [DataContract(Namespace = "http://numetaheuristics.codeplex.com/schemas")]
    [Export(typeof(IContext<DoubleVectorGenotype>))]
    public class Schwefel : DoubleVectorContext
    {
        public Schwefel()
            : base()
        {
        }

        public Schwefel(int length)
            : base(length, -512.03, 511.97)
        {
        }

        override public void Evaluate(ref DoubleVectorGenotype genome)
        {
            double total = 0.0;

            foreach (double x in genome.Vector)
            {
                double a = Math.Sqrt(Math.Abs(x));
                double b = -x * Math.Sin(a);

                total += b;
            }

            genome.Fitness = new FitnessSmallerIsBetter(total);
        }
    }
}
